package com.MRSyc.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.MRSyc.config.PageModel;
import com.MRSyc.entity.TRole;
import com.MRSyc.entity.TRoleResources;
import com.MRSyc.mapper.TRoleMapper;
import com.MRSyc.mapper.TRoleResourcesMapper;
import com.MRSyc.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.abel533.sql.SqlMapper;
import com.google.gson.Gson;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Fxq
 * @since 2021-01-12
 */
@Slf4j
@Controller
public class TRoleController {
	@Autowired
	private TRoleMapper roleServiceImpl;

	@Autowired
	private TRoleResourcesMapper roleResourcesService;

	@RequestMapping(value = "/role/list", method = { RequestMethod.GET })
	public String role_list() throws Exception {
		return "role/role_list";
	}

	@RequestMapping(value = "/role/list", method = { RequestMethod.POST })
	@ResponseBody
	public PageModel list(TRole role, PageModel<TRole> page) throws Exception {
		page.init();
		QueryWrapper<TRole> queryWrapper = new QueryWrapper<>();
		IPage<TRole> ipage = new Page<>(page.getCurrentPageNum(), page.getPageSize());
		IPage<TRole> list = roleServiceImpl.selectPage(ipage, queryWrapper);

		page.packData(list.getTotal(), list.getRecords());
		return page;
	}

	@RequestMapping(value = "/role/add", method = { RequestMethod.GET })
	public String addInit(TRole role, Model model) {
		return "role/role_add";
	}

	@RequestMapping(value = "/role/add", method = { RequestMethod.POST })
	@ResponseBody
	public Result add(TRole role) {
		role.setId(roleServiceImpl.getMaxId());
		roleServiceImpl.insert(role);
		return Result.genSuccessResult();
	}

	@RequestMapping(value = "/role/update", method = { RequestMethod.GET })
	public String updateInit(TRole role, Model model) {
		role = roleServiceImpl.selectById(role.getId());
		model.addAttribute("role", role);
		return "role/role_update";
	}

	@RequestMapping(value = "/role/update", method = { RequestMethod.POST })
	@ResponseBody
	public Result update(TRole role) {
		roleServiceImpl.updateById(role);
		return Result.genSuccessResult();
	}

	@RequestMapping(value = "/role/delete", method = { RequestMethod.POST })
	@ResponseBody
	public Result delete(TRole role) {
		roleServiceImpl.deleteById(role.getId());
		return Result.genSuccessResult();
	}

	@RequestMapping(value = "/role/roleResource", method = { RequestMethod.GET })
	public String roleResourceInit(TRole role, Model model) {
		role = roleServiceImpl.selectById(role.getId());
		// 根据role 查找roleResource表中，本角色管理的资源
		// SELECT t.id AS id, t.deptName AS `name`, t.parentId pId , 'true' AS `open`
		// FROM t_dept t
		String roleKey = role.getRoleKey();
		// 1.查询所有的资源列表
		// 2.查询角色对应的资源
		// 3.遍历所有资源列表，如果角色对应的资源列表中有，就给他标识为选中状态
		String sql = "    SELECT  res.resKey AS id, " + "    res.name AS name, " + "    res.presKey AS pId, "
				+ "    'true' AS open1,     "
				+ "    CASE WHEN   res.resKey IN ( SELECT r.resKey FROM t_role_resources r WHERE r.roleKey = "
				+ "  #{roleKey} )  THEN 'true' ELSE 'false' END AS checked  " + " FROM  t_resources res ";

		List<Map> selectList = sqlMapper.selectList(sql, roleKey, Map.class);

		model.addAttribute("treeNodes", toJson(selectList));
		model.addAttribute("roleKey", roleKey);
		return "role/role_resource";
	}

	@RequestMapping(value = "/role/roleResource", method = { RequestMethod.POST })
	@ResponseBody
	public Result roleResource(String resKeys, String roleKey, Model model) {
		log.info(resKeys);
		log.info(roleKey);
		if ("".equals(resKeys) || "".equals(roleKey))
			return Result.genSuccessResult();
		// 保存此roleKey对应的resKeys 到表t_role_resources
		String[] resKeysArr = resKeys.split("##");
		TRoleResources record = new TRoleResources();
		 record.setRoleKey(roleKey);
		 QueryWrapper<TRoleResources> wrapper=new QueryWrapper();
		 wrapper.setEntity(record);
		 roleResourcesService.delete(wrapper);
		//roleResourcesService.updateResKeys(resKeysArr, roleKey);
		 for(String resKey:resKeysArr) {
			 TRoleResources entity=new TRoleResources();
			 entity.setId(roleResourcesService.getMaxId());
			 entity.setResKey(resKey);
			 entity.setRoleKey(roleKey);
			 roleResourcesService.insert(entity);
		 }
		return Result.genSuccessResult();
	}

	@Autowired
	private SqlMapper sqlMapper;

	private Gson gson = new Gson();

	@RequestMapping(value = "/role/selectPageList", method = { RequestMethod.GET })
	public String selectPageList() {
		return "role/role_selectPageList";
	}

	@RequestMapping(value = "/role/selectPageList", method = { RequestMethod.POST })
	@ResponseBody
	public PageModel selectPageList(TRole role, PageModel<TRole> page) throws Exception {
		page.init();
		QueryWrapper<TRole> queryWrapper = new QueryWrapper<>();
		queryWrapper.setEntity(role);
		IPage<TRole> ipage = new Page<>(page.getCurrentPageNum(), page.getPageSize());
		IPage<TRole> list = roleServiceImpl.selectPage(ipage, queryWrapper);

		page.packData(list.getTotal(), list.getRecords());
		return page;
	}

	@RequestMapping(value = "/role/choosePage", method = { RequestMethod.GET })
	public String selectPage(String fkName, Model model) {
		if (StringUtils.isNotEmpty(fkName)) {
			Map<String, Object> foreignKeyMap = sqlMapper
					.selectOne("select * from t_fkeys where fkName = \'" + fkName + "\'");
			String rSql = foreignKeyMap.get("rSql").toString();
			log.info("rSql:{}", rSql);
			List<Map<String, Object>> relativeMapList = sqlMapper.selectList(rSql);
			if (relativeMapList != null) {
				model.addAttribute("relativeMapList", relativeMapList);
				String rType = foreignKeyMap.get("rType").toString();
				if ("0".equals(rType))
					return select_radioPage(model);
				if ("1".equals(rType))
					return select_radioPage(model);
				if ("2".equals(rType)) {
					String coverOtherValueColumn = foreignKeyMap.get("coverOtherValueColumn").toString();
					model.addAttribute("coverOtherValueColumn", coverOtherValueColumn);
					model.addAttribute("treeNodes", toJson(relativeMapList));
					return select_zTreePage(model);
				}
			} else {
				return "error/error";
			}
		}
		return "error/error";
	}

	private String select_radioPage(Model model) {
		model.addAttribute("demo", "demoValue");
		return "role/role_selectPageList";
	}

	private String select_zTreePage(Model model) {
		model.addAttribute("demo", "demoValue");
		return "role/role_foreignKeyTree";
	}

	public String toJson(Object obj) {
		String s = gson.toJson(obj);
		return s;
	}

}
